package com.pb.camera.jph.activity;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.journeyapps.barcodescanner.CaptureActivity;
import com.pb.camera.R;
import com.pb.camera.bean.DeviceMode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class AddCameraNoticeActivity extends Activity implements OnClickListener{

	private ImageButton ib_back_add;
	private TextView tv_add;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_camera_notice);
		initView();
		initData();
	}

	private void initView() {
		ib_back_add = (ImageButton) findViewById(R.id.ib_back_add);
		tv_add = (TextView) findViewById(R.id.tv_add);
	}

	private void initData() {
		ib_back_add.setOnClickListener(this);
		tv_add.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ib_back_add:
			finish();
			break;
		case R.id.tv_add:
			startCapture();
			break;
		}
	}

	private void startCapture() {
		IntentIntegrator intentIntegrator = new IntentIntegrator(this);
		intentIntegrator.setOrientationLocked(false);
		intentIntegrator.setCaptureActivity(MyCaptureActivity.class);
		intentIntegrator.initiateScan();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
		if(result != null) {
			if(result.getContents() == null) {
				Toast.makeText(this, R.string.cancel_capture, Toast.LENGTH_LONG).show();
			} else {
				Intent intent = new Intent(this,SettingCameraNameActivity.class);
				DeviceMode device = new DeviceMode();
				String contents = result.getContents();
				device.setDevtype(contents.substring(contents.length()-4,contents.length()));
				device.setDevid(contents.substring(0,contents.length()-4));
				intent.putExtra("device", device);
				startActivity(intent);
			}
		} else {
			// This is important, otherwise the result will not be passed to the fragment
			super.onActivityResult(requestCode, resultCode, data);
		}
	}
}
